var express = require("express");
var app = express();
var fs = require('fs');

app.use(express.static("public"));


//教师注册
if (!fs.existsSync('./teacher.json')) {
    fs.writeFileSync('./teacher.json',[])
  }

var Tuserlist=JSON.parse(fs.readFileSync('./teacher.json','utf-8')||"[]");
app.get("/TeaReg", function(req, res) {
    var userpass = req.query.userpass;
    var userpasstwo = req.query.userpasstwo;
  
    for (var j = 0; j < Tuserlist.length; j++) {
      if (Tuserlist[j].username == req.query.username) {
        res.send("账号已被注册");
        return;
      }
    }
    Tuserlist.push({
      username: req.query.username,
      userpass: req.query.userpass,
      userpasstwo: req.query.userpasstwo,
    });
  
    if (userpass == userpasstwo) {
      var TjsonStr = JSON.stringify(Tuserlist);
      fs.writeFileSync('./teacher.json', TjsonStr);
      return res.redirect("index.html");
    } else {
      res.send('<script>alert("密码不一致");</script>');
    }
  
  });

  //教师登录
  app.get("/login", function(req, res) {
    var username = req.query.username;
    var userpass = req.query.userpass;
  
    if (username=='') {
      res.send('账号为空')
    }
    var mes='账号不存在'
  
    for (var i = 0; i < Tuserlist.length; i++) {
      if (Tuserlist[i].username == username) {
        if (Tuserlist[i].userpass == userpass) {
          // console.log(userlist);
          // res.send(getString(userlist[i]));
          // res.send('登录成功');
          mes=getString(Tuserlist,Suserlist)
          break;
        } else {
          mes='密码错误'
        }
      } else {    
        mes='账号不存在'
      }
    }
    res.send(mes)
  });

  //学生信息添加
  if (!fs.existsSync('./student.json')) {
    fs.writeFileSync('./student.json',[])
  }

  var Suserlist=JSON.parse(fs.readFileSync('./student.json','utf-8')||"[]");
  app.get("/stuadd", function(req, res) {

    var stunum=202030000
    if (Suserlist.length !== 0) {
        stunum=(Suserlist[Suserlist.length-1].stunum) +1;
    }
    Suserlist.push({
      stunum:stunum,
      username: req.query.username,
      userpass: req.query.University,
      userpasstwo: req.query.userpasstwo,
      age: req.query.age,
      sex: req.query.sex,
      major: req.query.major,
      love: req.query.love
    });


  
      var SjsonStr = JSON.stringify(Suserlist);
      fs.writeFileSync('./student.json', SjsonStr);
      return res.send(getString(Tuserlist,Suserlist));
   
  });




function getString(data,list) {
  // console.log(data);

  var liststr=``

  if (!list=='') {
       for(var i = 0; i < list.length; i++){
    liststr +=`
    <tr>
    <td>${list[i].stunum}</td>
    <td>${list[i].username}</td>
    <td>${list[i].University}</td>
    <td>${list[i].age}</td>
    <td>${list[i].sex}</td>
    <td>${list[i].major}</td>
    <td>${list[i].love}</td>
    </tr>
    `
  }
  }
 

  

  var str = `
        <!DOCTYPE html>
        <html lang="zh-CN">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
            <meta name="author" content="林镭特">
            <title></title>
        </head>
        <body>
    `;
  str += `

  
  <p>管理员：${data[0].username}</p>



  <button><a href="./StuAdd.html">添加</a></button>
      
        <table>
            <tr>
                <td>学号</td>
                <td>姓名</td>
                <td>大学</td>
                <td>年龄</td>
                <td>性别</td>
                <td>专业</td>
                <td>爱好</td>

            </tr>
            ${liststr}
        </table>
        `;
  str += `</body></html>`;
  return str;
}

console.log(`
一、注册
    1、账号重名判断10分        10
    2、两次密码不一致判断10分   10
    3、注册后账号文件存储10分   10
二、登录
    1、用户名是否存在判断10分    10
    2、密码是否正确判断10分      10
三、添加学生
    1、学生信息本地存储10分      10
    2、学生学号自动增长10分      10
四、学生列表
    1、学生列表信息展示30分      29

    总分                        99

`);




app.listen(5555, function() {
    console.log("服务器启动...");
  });